var should = require('should');
var _ = require('lodash');
var chestHandler = require('../../../app/servers/bag/handler/chestHandler');
var bagModel = require('../../../app/models/bagModel');
var config = require('../../config');
var configUtil = require('../../../app/util/configUtil');
var analyze = require('kc-analyze');

suite('初始化', function () {
    test('analyze', function (done) {
        var analyze_config = configUtil.load('analyze');
        var game_config = configUtil.load('game');
        analyze.initialize({
            host: analyze_config.host,
            server_id: game_config.server_id
        });
        done();
    });
});

suite('测试chestHandler', function () {
    suite('use', function () {
        test('初始化', function (done) {
            var analyze_config = configUtil.load('analyze');
            var game_config = configUtil.load('game');
            analyze.initialize({
                host: analyze_config.host,
                server_id: game_config.server_id
            });
            done();
        });

        test('打开背包宝箱', function (done) {
            var msg = {};
            var session = {};
            session.uid = config.other_role_id;

            var chest_handler = new chestHandler();
            chest_handler.use(msg, session, function (err, res) {
                done();
            });
        });

        test('打开背包宝箱', function (done) {
            var msg = {
                property_id: 30401
            };
            var session = {};
            session.uid = config.other_role_id;

            var chest_handler = new chestHandler();
            chest_handler.use(msg, session, function (err, res) {
                done();
            });
        });
    });

    suite('打开宝箱', function () {
        test('初始化', function (done) {
            var analyze_config = configUtil.load('analyze');
            var game_config = configUtil.load('game');
            analyze.initialize({
                host: analyze_config.host,
                server_id: game_config.server_id
            });
            done();
        });

        setup(function (done) {
            var analyze_config = configUtil.load('analyze');
            var game_config = configUtil.load('game');
            analyze.initialize({
                host: analyze_config.host,
                server_id: game_config.server_id
            });

            bagModel.getByRoleId(config.other_role_id, function (err, bag_model) {
                var property_type = 30401;
                bag_model.addProperty(property_type, 1);
                bag_model.save(done);
            });
        });

        test('打开背包宝箱', function (done) {
            var property_id;
            bagModel.getByRoleId(config.other_role_id, function (err, bag_model) {
                var property_list = bag_model.getPropertyList();
                _.some(property_list, function (property_item) {
                    if (property_item.property_type == 30401) {
                        property_id = property_item.property_id;
                        return;
                    }
                });
                bag_model.save(function () {
                    var msg = {
                        property_id: property_id
                    };
                    var session = {};
                    session.uid = config.other_role_id;

                    var chest_handler = new chestHandler();
                    chest_handler.use(msg, session, function (err, res) {
                        done();
                    });
                });
            });
        });
    });

    suite('打开固定宝箱', function () {
        test('初始化', function (done) {
            var analyze_config = configUtil.load('analyze');
            var game_config = configUtil.load('game');
            analyze.initialize({
                host: analyze_config.host,
                server_id: game_config.server_id
            });
            done();
        });

        setup(function (done) {
            bagModel.getByRoleId(config.other_role_id, function (err, bag_model) {
                var property_type = 30101;
                bag_model.addProperty(property_type, 1);
                bag_model.save(done);
            });
        });

        test('打开背包固定宝箱', function (done) {
            var property_id;
            bagModel.getByRoleId(config.other_role_id, function (err, bag_model) {
                var property_list = bag_model.getPropertyList();
                _.some(property_list, function (property_item) {
                    if (property_item.property_type == 30101) {
                        property_id = property_item.property_id;
                        return;
                    }
                });
                bag_model.save(function () {
                    var msg = {
                        property_id: property_id
                    };
                    var session = {};
                    session.uid = config.other_role_id;

                    var chest_handler = new chestHandler();
                    chest_handler.use(msg, session, function (err, res) {
                        done();
                    });
                });
            });
        });
    });
});
